using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyTools;

/// <summary>
/// 挂载在蛊上: 负责寻找作用对象
/// 可供继承使用的成员:
/// Rigidbody2D rb: 用与进行运动控制
/// Collider2D co: 用与进行碰撞检测，在Awake函数中初始化
/// SpriteRenderer sr: 用与进行渲染
/// List<GameObject> findedGameObjects: 用与存储找到的对象
/// 函数: 
/// protected vitrual void Awake(): 默认实现为获取或添加组件
/// protected virtual void Start(): 默认实现一个一段时间后自动End的功能
/// protected virtual void Update(): 默认实现为空 
/// protected virtual void OnTriggerEnter2D(Collider2D other): 默认实现为添加对象到findedGameObjects中
/// protected virtual void OnTriggerExit2D(Collider2D other): 默认实现为空
/// protected virtual void End(): 默认实现为清空findedGameObjects并将自己销毁(BaseStartCondition一般情况下也会在FindTarget结束时进行结束)
/// protected virtual void SetTarget( GameObject target ): 默认实现为在findedGameObjects中添加对象
/// protected virtual List<GameObject> GetTargets(): 暴露给其他类获取findedGameObjects的函数
/// </summary>
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(SpriteRenderer))]
[RequireComponent(typeof(Collider2D))]
public class BaseFindTarget : MonoBehaviour
{
    protected Rigidbody2D rb;
    protected Collider2D co;
    protected SpriteRenderer sr;

    protected  List<GameObject> findedGameObjects = new List<GameObject>();

    protected virtual void Awake()
    {
        rb = GetComponent<Rigidbody2D>();

        if (GetComponent<Collider2D>()==null){
            co = gameObject.AddComponent<BoxCollider2D>();
        }else
            co = GetComponent<Collider2D>();

        if (GetComponent<SpriteRenderer>()==null){
            sr = gameObject.AddComponent<SpriteRenderer>();
        }else
            sr = GetComponent<SpriteRenderer>();
    }

    protected virtual void Start(){
        Cooldown cooldown = new Cooldown(5, ()=>{
            End();
        });
        cooldown.StartCoolDown();// 5s后自动销毁
    }

    protected virtual void Update(){}

    protected virtual void SetTarget( GameObject target ){
        findedGameObjects.Add(target);
    }

    public virtual List<GameObject> GetTargets(){
        return findedGameObjects;
    }

    protected virtual void ClearTarget(){
        findedGameObjects.Clear();
    }


    protected virtual void OnTriggerEnter2D(Collider2D collider2D){
        if (collider2D.tag=="Enemy"){
            SetTarget(collider2D.gameObject);
        }
    }
    protected virtual void OnTriggerExit2D(Collider2D collider2D){}

    protected virtual void End(){
        gameObject.SetActive(false);
        Destroy(gameObject);
    }
}
                                                                                                                